<template>
  <div class="enterpriseExecutive">
    <div class="form">
      <el-form model="form" inline>
        <el-form-item label="高管姓名">
          <el-input v-model="form.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="add">
      <el-button type="primary" @click="addIndustry">添加高管</el-button>
    </div>
    <div class="table">
      <el-table style="height: calc(100vh - 230px)" :data="tableData">
        <el-table-column type="index" width="60" label="序号"></el-table-column>
        <el-table-column
          prop="name"
          align="center"
          show-overflow-tooltip
          label="高管姓名"
        ></el-table-column>
        <el-table-column
          prop="enterprise.name"
          align="center"
          show-overflow-tooltip
          label="企业名称"
        ></el-table-column>
        <el-table-column prop="photo" align="center" show-overflow-tooltip label="头像">
          <template v-slot:default="scope">
            <div v-if="scope.row.photo">
              <img
                :src="`http://localhost:3005/${scope.row.photo}`"
                alt="头像"
                width="50"
                height="50"
              />
            </div>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="education"
          align="center"
          show-overflow-tooltip
          label="学历"
        ></el-table-column>
        <el-table-column
          prop="position"
          align="center"
          show-overflow-tooltip
          label="职位"
        ></el-table-column>

        <el-table-column prop="operation" label="操作">
          <template #default="scope">
            <el-button type="text" @click="editExecutive(scope.row)">编辑</el-button>
            <el-button type="text" @click="deleteExecutive(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="tableData.length" class="pagination-w">
        <el-pagination
          :total="searchParams.total"
          background
          :locale="zhCn"
          :current-page="searchParams.current"
          :page-size="searchParams.pageSize"
          :page-sizes="searchParams.pageSizes"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <AddExecutiveDialog ref="addExecutiveDialog" @submit="getExecutiveData" />
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { getExecutiveList, deleteExecutiveAPI } from '@/api/enterprise'
import AddExecutiveDialog from './addExecutiveView.vue'
import { ElMessage } from 'element-plus'

const form = reactive({
  name: '',
})
const tableData = ref([])
const searchParams = ref({
  current: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 200],
  total: 0,
})
const addExecutiveDialog = ref(null)
const search = async () => {
  const res = await getExecutiveList(
    form.name,
    searchParams.value.current,
    searchParams.value.pageSize
  )
  if (res.status === 200 || res.status === 201) {
    tableData.value = res.data.data.executive
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}

const editExecutive = (row) => {
  addExecutiveDialog.value?.openDialog(row)
}

const addIndustry = () => {
  addExecutiveDialog.value?.openDialog()
}

const getExecutiveData = async () => {
  const res = await getExecutiveList(
    form.name,
    searchParams.value.current,
    searchParams.value.pageSize
  )
  if (res.status === 200 || res.status === 201) {
    tableData.value = res.data.data.executive
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}
const handleSizeChange = (newSize) => {
  console.log('size change', newSize)
  searchParams.value.pageSize = newSize
  getExecutiveData()
}

const deleteExecutive = async (row) => {
  const res = await deleteExecutiveAPI(row.id)
  if (res.status === 200 || res.status === 201) {
    ElMessage.success('删除成功')
    getExecutiveData()
  }
}

const handlePageChange = (newPage) => {
  console.log('page change', newPage)
  searchParams.value.current = newPage
  getExecutiveData()
}
onMounted(() => {
  getExecutiveData()
})
</script>

<style scoped lang="scss">
.enterpriseExecutive {
  padding: 30px;
  position: relative;
  .add {
    position: absolute;
    top: 30px;
    right: 30px;
  }
  .table {
    .pagination-w {
      display: flex;
      justify-content: right;
      margin-top: 20px;
    }
  }
}
</style>
